package com.skit.zixing;

import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.skit.common.base.BaseActivity;
import com.skit.common.bean.User;
import com.skit.common.config.AppConfig;
import com.skit.common.manager.UserManager;
import com.skit.common.utils.ToastUtils;

import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import retrofit2.Retrofit;

@AndroidEntryPoint
public class QRCodeScanActivity extends BaseActivity {

    private CaptureManager capture;
    private DecoratedBarcodeView bv_barcode;
    private String results = "";

    @Inject
    Retrofit retrofit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            getWindow().setStatusBarColor(0xffffffff);
//            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//        }
        setContentView(R.layout.activity_zixing_layout);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        setTitle("扫一扫");
        bv_barcode = findViewById(R.id.bv_barcode);

        capture = new CaptureManager(this, bv_barcode);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
//        capture.decode();
        bv_barcode.decodeContinuous(new BarcodeCallback() {
            @Override
            public void barcodeResult(BarcodeResult result) {
                bv_barcode.resume();
                if (result != null) {
                    String text = result.getText();
                    if (!results.equals(text)) {
                        results = text;
                        checkQRCodeType(text);
                    }
                    bv_barcode.resume();
                }
            }

            @Override
            public void possibleResultPoints(List<ResultPoint> resultPoints) {

            }
        });
    }

    private void checkQRCodeType(String text) {
        if (text != null && !text.isEmpty()) {
            if (text.startsWith(AppConfig.BASE_HOST) && text.indexOf("uuid=") > 0) {
                User user = UserManager.getUser();
                if (user == null) {
                    ToastUtils.showToast("未登录无法使用扫码登录功能");
                    return;
                }
                final String uuid = text.substring(text.indexOf("uuid=") + "uuid=".length());
                showLoginDialog(uuid, user);
            }
        }
    }

    private void showLoginDialog(final String uuid, User user) {
        ScanLoginAlertDialog.show(this, retrofit, uuid, user);
    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return bv_barcode.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

/*    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("扫码结果分析", "Cancelled");
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "扫描结果为空", Toast.LENGTH_LONG).show();
            } else {
                String str = result.getContents();
                Toast.makeText(this, str, Toast.LENGTH_LONG).show();
            }
        }
    }*/
}
